<template>
  <el-dialog :title="!form.id ? '新增' : '修改'" :close-on-click-modal="false" width="600px" :visible.sync="visible">
    <el-form :model="form" size="mini" :rules="rules" ref="form" @keyup.enter.native="submitForm()"
             label-width="80px">
      <el-form-item label="品牌名" prop="name">
        <el-input v-model="form.name" placeholder="品牌名"></el-input>
      </el-form-item>
      <el-form-item label="logo" prop="logo">
        <material-library :width="120" :height="50" v-model="form.logo" :num="1"></material-library>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="form.description" placeholder=""></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-switch v-model="form.status"
                       :active-value="1"
                       :inactive-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="form.sort" placeholder=""></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" icon="el-icon-circle-check" @click="submitForm()">确定</el-button>
      <el-button @click="visible = false" size="mini" icon="el-icon-circle-close">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import MaterialLibrary from "@/components/ImageLibrary/index.vue";
import {getBrandById, saveBrand, updateBrand} from "@/api/product/brand";

export default {
  components: {MaterialLibrary},
  data() {
    return {
      form: {},
      visible: false,
      rules: {
        name: [
          {required: true, message: "品牌名称不能为空", trigger: "blur"}
        ],
        logo: [
          {required: true, message: "请上传logo", trigger: "blur"}
        ],
      },
    }
  },
  methods: {
    add() {
      this.form = {
        status: 1,
        sort: 1,
      }
      this.visible = true
      this.title = "添加品牌";
    },
    edit(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
        getBrandById(id).then(res => {
          if (res.success) {
            this.form = res.data
          }
        });
      });
    },
    // 表单提交
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (!this.form.id) {
            saveBrand(this.form).then(res => {
              if (res.success) {
                this.visible = false
                this.$message.success("新增成功")
                this.$emit('ok')
              }
            });
          } else {
            updateBrand(this.form).then(res => {
              if (res.success) {
                this.visible = false
                this.$message.success("修改成功")
                this.$emit('ok')
              }
            });
          }
        }
      });
    },
  }
}
</script>
